/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.model;

import java.util.Calendar;
import java.util.Date;

import org.geoscheduler.commons.marshaller.JSONField;
import org.geoscheduler.commons.tools.StringTools;

/**
 * Token used for authorization of REST requests.
 *
 * @author srakyi
 */
public class Token implements Identifiable {
	
	private String tokenId;
	private Date validUntil;
	private String salt;
	private Long idAccount;
	
	/**
	 * Empty constructor is meant to be used only for creating TEMPORARY object that will be
	 * filled by data from database right after creation!
	 */
	public Token() {
		super();
	}
	
	public Token(String tokenId, String salt, Long idAccount, Date validUntil) {
		super();
		this.idAccount = idAccount;
		this.salt = salt;
		this.tokenId = tokenId;
		this.validUntil = validUntil;
	}

	@JSONField
	public Long getId() {
		return idAccount;
	}

	public void setId(Long idAccount) {
		this.idAccount = idAccount;
	}


	public void extendValidation(int addSeconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(this.validUntil);
		cal.add(Calendar.SECOND, addSeconds);
		
		this.validUntil = cal.getTime();
	}
	
	public static String computeSalt(Account acc, String id) {
		return StringTools.sha1sum(acc.getLogin() + acc.getPasswd() + id);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		
		if (!(obj instanceof Token)) {
			return false;
		}
		Token t = (Token)obj;
		
		if ((!this.getId().equals(t.getId())) ||
			(!this.getSalt().equals(t.getSalt())) ||
			(!this.getIdAccount().equals(t.getIdAccount())) ||
			// using toString() because we need to compare only with precision on seconds (not milis)
			(!this.getValidUntil().toString().equals(t.getValidUntil().toString()))) {
			return false;
		}
		
		return true;
	}

// Getters & Setters
	
	@JSONField
	public String getTokenId() {
		return tokenId;
	}

	public void setTokenId(String tokenId) {
		this.tokenId = tokenId;
	}

	@JSONField
	public Date getValidUntil() {
		return validUntil;
	}

	public void setValidUntil(Date validUntil) {
		this.validUntil = validUntil;
	}

	@JSONField
	public Long getIdAccount() {
		return idAccount;
	}

	public void setIdAccount(Long idAccount) {
		this.idAccount = idAccount;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}
}
